package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.mapper.ClazzTeacherMapper;
import com.demo.pojo.entity.ClazzTeacher;
import com.demo.service.ClazzTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 班级-教师关联服务实现类
 */
@Service
public class ClazzTeacherServiceImpl implements ClazzTeacherService {
    
    @Autowired
    private ClazzTeacherMapper clazzTeacherMapper;
    
    @Override
    public boolean addClazzTeacher(ClazzTeacher clazzTeacher) {
        return clazzTeacherMapper.insert(clazzTeacher) > 0;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addClazzTeachers(Long clazzId, List<Long> teacherIds, Integer roleType) {
        if (teacherIds == null || teacherIds.isEmpty()) {
            return true;
        }
        
        for (Long teacherId : teacherIds) {
            ClazzTeacher clazzTeacher = new ClazzTeacher();
            clazzTeacher.setClazzId(clazzId);
            clazzTeacher.setTeacherId(teacherId);
            clazzTeacherMapper.insert(clazzTeacher);
        }
        
        return true;
    }
    
    @Override
    public boolean deleteClazzTeacher(Long id) {
        QueryWrapper<ClazzTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazz_id", id).or().eq("teacher_id", id);
        return clazzTeacherMapper.delete(queryWrapper) > 0;
    }
    
    @Override
    public boolean deleteByClazzId(Long clazzId) {
        QueryWrapper<ClazzTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazz_id", clazzId);
        return clazzTeacherMapper.delete(queryWrapper) >= 0;
    }
    
    @Override
    public boolean deleteByTeacherId(Long teacherId) {
        QueryWrapper<ClazzTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", teacherId);
        return clazzTeacherMapper.delete(queryWrapper) >= 0;
    }
    
    @Override
    public boolean updateClazzTeacher(ClazzTeacher clazzTeacher) {
        QueryWrapper<ClazzTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazz_id", clazzTeacher.getClazzId())
                    .eq("teacher_id", clazzTeacher.getTeacherId());
        return clazzTeacherMapper.update(clazzTeacher, queryWrapper) > 0;
    }
    
    @Override
    public List<Long> getTeachersByClazzId(Long clazzId) {
        return clazzTeacherMapper.getTeacherIdsByClazzId(clazzId);
    }
    
    @Override
    public List<Long> getClazzesByTeacherId(Long teacherId) {
        return clazzTeacherMapper.getClazzIdsByTeacherId(teacherId);
    }
    
    @Override
    public Long getTeacherByClazzIdAndRole(Long clazzId, Integer roleType) {
        // 由于表中没有role_type字段，返回第一个教师
        List<Long> teachers = getTeachersByClazzId(clazzId);
        return teachers.isEmpty() ? null : teachers.get(0);
    }
}

